package study.alex.resumedb.gae.model;

import com.vaadin.addon.jpacontainer.JPAContainerFactory;
import study.alex.resumedb.gae.EMF;
import study.alex.resumedb.gae.ResumeBlob;
import javax.persistence.*;
import javax.persistence.jpa21.Convert;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.logging.Logger;

/**
 * Created with IntelliJ IDEA.
 * User: alex
 * Date: 07.11.13
 * Time: 12:10
 * SVN $Id$
 */

@Entity
public class Resume implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @javax.persistence.Column(name="RESUME_ID")
    @org.datanucleus.api.jpa.annotations.Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
    private String id;

    private String userId = null;

    private String givenName = "";
    private String givenPhones = "";
    private String givenEmails = "";
    private int salary = 0;
    private String position = "";

/*
    @OneToOne (fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "resume")
    private GoogleDriveResumeBlob body;
*/
    private String bodyId;

    @OneToMany (mappedBy = "resume", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name="RESUME_ID")
    private List<resumeTracker> history = new ArrayList<resumeTracker>();

    @Basic
    @Convert(converter = GregorianCalendarConverter.class)
    private GregorianCalendar date;

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }
    public String getGivenName() {
        return givenName;
    }

    public void setGivenName(String givenName) {
        this.givenName = givenName;
    }

    public String getGivenPhones() {
        return givenPhones;
    }

    public void setGivenPhones(String givenPhones) {
        this.givenPhones = givenPhones;
    }

    public String getGivenEmails() {
        return givenEmails;
    }

    public void setGetGivenEmails(String givenEmails) {
        this.givenEmails = givenEmails;
    }

    public String getId() {
        return id;
    }

    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }

    public String getPosition() {
        return position;
    }

    public void setPosition(String position) {
        this.position = position;
    }

    public byte[] getContent() {
        if (bodyId == null || bodyId.isEmpty()) return null;
        ResumeBlob blob = getBlobInstance(bodyId);
        try {
            return blob != null ? blob.getBlob() : null;
        } catch (Exception e) {
            Logger.getLogger(Resume.class.getName()).warning(e.toString());
            return null;
        }
    }

    public ResumeBlob getBody() {
        if (bodyId == null || bodyId.isEmpty()) return null;
        return getBlobInstance(bodyId);
    }

    public void setBody(ResumeBlob blobObj) {
        if (blobObj != null) bodyId = blobObj.getId();
    }

    public String getType() {
        if (bodyId == null || bodyId.isEmpty()) return null;
        ResumeBlob blob = getBlobInstance(bodyId);
        return (blob == null) ? null : blob.getType();
    }

    public Resume(String userId, Mine givenContact, int salary, String position) {
        this.givenName = givenContact.getName();
        this.givenPhones = givenContact.getPhones();
        this.givenEmails = givenContact.getEmails();
        this.salary = salary;
        this.position = position;
        this.userId = userId;
        this.date = new GregorianCalendar();
    }

    public ResumeBlob getBlobInstance(String id) {
        if (id == null || id.isEmpty()) return null;

        EntityManager em = JPAContainerFactory.createEntityManagerForPersistenceUnit(EMF.PERSISTENCE_UNIT);

        try {
            ResumeBlob bl = em.find(GoogleDriveResumeBlob.class, id);
            Logger.getLogger(Resume.class.getName()).fine("Got BLOB for Id (" + id + ")");
            return bl;
        } finally {
            em.close();
        }
    }

    public List<resumeTracker> getHistory() {

        return history;

    }

    public void setHistory(List<resumeTracker> history) {
        this.history = history;
    }

    public long getContentSize() {
        if (bodyId == null || bodyId.isEmpty()) return -1;

        ResumeBlob blob = getBlobInstance(bodyId);
        if (blob != null) {
            try {
                long sz = blob.getSize();
                return sz;
            } catch (Exception e) {
                Logger.getLogger(Resume.class.getName()).fine(e.toString());
                return -1;
            }
        }
        return -1;
    }

    public GregorianCalendar getDate() {
        return date;
    }

    public void setDate(GregorianCalendar date) {
        this.date = date;
    }
}
